﻿using System;
using System.Collections.Generic;

namespace WiMo.Games
{
    public struct Rectangle
    {
        private List<Location> _corners;
        private int _x, _y, _width, _height;
        public static readonly Rectangle Empty;
        public Rectangle(int x, int y, int width, int height)
        {
            _x = x;
            _y = y;
            _width = width;
            _height = height;
            _corners = new List<Location>();
        }

        public Rectangle(float x, float y, float width, float height)
        {
            _x = (int)x;
            _y = (int)y;
            _width = (int)width;
            _height = (int)height;
            _corners = new List<Location>();
        }

        public List<Location> Corners
        {
            get
            {
                _corners = new List<Location>();
                _corners.Add(new Location(Top, Left));
                _corners.Add(new Location(Top, Right));
                _corners.Add(new Location(Bottom, Left));
                _corners.Add(new Location(Bottom, Right));

                return _corners;
            }
        }

        public static bool operator !=(Rectangle left, Rectangle right)
        {
            if (left.Top != right.Top)
                return true;

            if (left.Left != right.Left)
                return true;

            if (left.Width != right.Width)
                return true;

            if (left.Height != right.Height)
                return true;

            return false;
        }
        public static bool operator ==(Rectangle left, Rectangle right)
        {
            return !(left != right);
        }

        public int Bottom { get { return Top + Height; } }

        public int Height { get { return _height; } set { _height = value; } }
        public bool IsEmpty
        {
            get
            {
                if (Height != 0)
                    return false;
                if (Width != 0)
                    return false;
                if (Top != 0)
                    return false;
                if (Left != 0)
                    return false;

                return true;
            }
        }

        public int Left { get { return _x; } }
        public Location Location { get { return new Location(_x, _y); } set { _x = value.X; _y = value.Y; } }
        public int Right { get { return Left + Width; } }
        public Size Size { get { return new Size(Width, Height); } set { Width = value.Width; Height = value.Height; } }
        public int Top { get { return _y; } }
        public int Width { get { return _width; } set { _width = value; } }
        public int X { get { return _x; } set { _x = value; } }
        public int Y { get { return _y; } set { _y = value; } }

        public bool IntersectsWith(Rectangle rect)
        {
            if (this.Bottom < rect.Top)
                return false;

            if (this.Right < rect.Left)
                return false;

            if (this.Top > rect.Bottom)
                return false;

            if (this.Left > rect.Right)
                return false;

            return true;
        }

        public bool Contains(Location point)
        {
            return ((point.X >= Left && point.X <= Right) && (point.Y >= Top && point.Y <= Bottom));
        }

        public override bool Equals(object obj)
        {
            return this == (Rectangle)obj;
        }

        public static Rectangle operator +(Rectangle rect, Location point)
        {
            return new Rectangle(rect.Left + point.X, rect.Top + point.Y, rect.Width, rect.Height);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return string.Format("Left={0} Top={1} Right={2} Bottom={3}", Left, Top, Right, Bottom);
        }

        public static Rectangle operator *(Rectangle left, Vector2 right)
        {
            return new Rectangle(left.X * right.X, left.Y * right.Y, left.Width * right.X, left.Height * right.Y);
        }

        public Rectangle GetIntersection(Rectangle rect)
        {
            var newRect = new Rectangle();
            newRect._x = Math.Max(this.Left, rect.Left);
            newRect._y = Math.Max(this.Top, rect.Top);

            var right = Math.Min(this.Right, rect.Right);
            var bottom = Math.Min(this.Bottom, rect.Bottom);

            newRect.Width = right - newRect._x;
            newRect.Height = bottom - newRect._y;
            
            return newRect;
        }
    }
}
